﻿using AutoMapper;
using DaoService.Model;
using NewUI.Models;

namespace NewUI.AutoMapper;

public class PlcListMapper
{
    
    /*// 基础映射（属性名相同）
    CreateMap<UserEntity, UserModel>()
    // 自定义映射（不同属性名）
    .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.Id))
    // 组合字段
    .ForMember(dest => dest.FullName, 
    opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
    // 格式化日期
    .ForMember(dest => dest.CreatedDate,
    opt => opt.MapFrom(src => src.CreatedAt.ToString("yyyy-MM-dd")))
    // 忽略敏感字段（不需要映射）
    .ForMember(dest => dest.PasswordHash, opt => opt.Ignore());*/
    
    // 配置映射
    private static MapperConfiguration configToModel = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<PlcInfoEntity, PlcList>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
            .ForMember(dest => dest.CpuType, opt => opt.MapFrom(src => src.CpuType))
            .ForMember(dest => dest.IpAddress, opt => opt.MapFrom(src => src.IpAddress))
            .ForMember(dest => dest.Rack, opt => opt.MapFrom(src => src.Rack))
            .ForMember(dest => dest.Slot, opt => opt.MapFrom(src => src.Slot))
            .ForMember(dest => dest.Timeout, opt => opt.MapFrom(src => src.Timeout));
    });

    private static MapperConfiguration configToEntity = new MapperConfiguration(cfg => {
        cfg.CreateMap<PlcList,PlcInfoEntity>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
            .ForMember(dest => dest.CpuType, opt => opt.MapFrom(src => src.CpuType))
            .ForMember(dest => dest.IpAddress, opt => opt.MapFrom(src => src.IpAddress))
            .ForMember(dest => dest.Rack, opt => opt.MapFrom(src => src.Rack))
            .ForMember(dest => dest.Slot, opt => opt.MapFrom(src => src.Slot))
            .ForMember(dest => dest.Timeout, opt => opt.MapFrom(src => src.Timeout));
    });
    
    public static IMapper ToModelMapper
    {
        get
        {
            return configToModel.CreateMapper();
        }
    }
    public static IMapper ToEntityMapper
    {
        get
        {
            return configToEntity.CreateMapper();
        }
    }
    
    
}